require('dotenv').config()
const Hapi = require('@hapi/hapi')
const Plugins = require('./server/plugins/index');
const AuthJwt = require('./server/plugins/jwt')
const Jwt = require('hapi-auth-jwt2');
const server = Hapi.server({
  load: {
    sampleInterval: 1000 //  the frequency of sampling in milliseconds.
  },
  port: 8002,
  host: '0.0.0.0',
  routes: {
    cors: {
      origin: [
        'http://admin.fortuna.local',
        'http://localhost:8088',
        'http://127.0.0.1:3000',
        'http://localhost:3000',
        'http://xj.xsysq123.cn',
        'http://adminxj.xsysq123.cn'
      ]
    },
    validate: {
      failAction: (request, h, err) => {
        throw err
      },
    }
  },
  debug: {
    log: ['*'],
    request: ['*']
  }
})
const init = async () => {
  await server.register(Plugins);
  await server.register(Jwt)
  await server.auth.strategy('jwt', 'jwt', {
    key: AuthJwt.key,
    validate: AuthJwt.validate,
    verifyOptions: AuthJwt.verifyOptions
  });
  await server.auth.default('jwt');
  await server.start();
  console.log('Server running on %s', server.info.uri);
}
init();
